﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Zen.Barcode;

namespace Worktool
{
    public partial class DmxDevices : Form
    {
        String pkInstalledPrinters;
        PrintDocument label;               
        
        public DmxDevices()
        {
            InitializeComponent();
            this.Text = "DMX Devices: " + Properties.Settings.Default.CurrentDBName; 
            this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
            
        }

        private void DmxDevices_Load(object sender, EventArgs e)
        {
            this.sTAMMDATEN_areasTableAdapter.Fill(this.aSCDataSet.STAMMDATEN_areas);
            this.vw_eng_dmxDevicesTableAdapter.updatedmxDevices();
            this.vw_eng_dmxDevicesTableAdapter.Fill(this.aSCDataSet.vw_eng_dmxDevices);
            // Drucker in das Auswahlfeld eintragen
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
                comboBox3.Items.Add(pkInstalledPrinters);
            }
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                if (Properties.Settings.Default.LabelPrinterName == PrinterSettings.InstalledPrinters[i])
                {
                    comboBox3.Text = Properties.Settings.Default.LabelPrinterName;
                }
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != dataGridView1.Columns["updateUserDataGridViewTextBoxColumn"].Index || e.ColumnIndex != dataGridView1.Columns["updateDateDataGridViewTextBoxColumn"].Index)
            {
                try
                {
                    dataGridView1.CurrentRow.Cells["updateDateDataGridViewTextBoxColumn"].Value = DateTime.Today;
                    dataGridView1.CurrentRow.Cells["updateUserDataGridViewTextBoxColumn"].Value = Properties.Settings.Default.User;
                    this.vwengdmxDevicesBindingSource.EndEdit();
                    vw_eng_dmxDevicesTableAdapter.updateDMX(dataGridView1.CurrentRow.Cells["universeDataGridViewTextBoxColumn"].Value.ToString(), Convert.ToInt32(dataGridView1.CurrentRow.Cells["dmxAdressDataGridViewTextBoxColumn"].Value), dataGridView1.CurrentRow.Cells["updateUserDataGridViewTextBoxColumn"].Value.ToString(), dataGridView1.CurrentRow.Cells["updateDateDataGridViewTextBoxColumn"].Value.ToString(), Convert.ToInt32(dataGridView1.CurrentRow.Cells["fixture"].Value), Convert.ToInt32(dataGridView1.CurrentRow.Cells["IDDataGridViewTextBoxColumn"].Value));
                    //vw_eng_dmxDevicesTableAdapter.updateMaterialDetail(dataGridView1.CurrentRow.Cells["SeriennummerDataGridViewTextBoxColumn"].Value.ToString(), Convert.ToInt32(dataGridView1.CurrentRow.Cells["fkmaterialDetailIDDataGridViewTextBoxColumn"].Value));
                    //vw_eng_dmxDevicesTableAdapter.Update(aSCDataSet);

                }
                catch (Exception ex)
                {
                }
            }
        }

        

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            dataGridView1.SelectAll();
            DataObject dataObj = dataGridView1.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlexcel = new Microsoft.Office.Interop.Excel.Application();
            xlexcel.Visible = true;
            xlWorkBook = xlexcel.Workbooks.Add(misValue);
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
            CR.Select();
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            dataGridView1.ClearSelection();
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            vwengdmxDevicesBindingSource.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}'", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"));
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            vwengdmxDevicesBindingSource.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}'", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"));
      
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            vwengdmxDevicesBindingSource.Filter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}' AND Model LIKE '{2}'", (textBox1.Text + "*"), (textBox2.Text + "*"), (textBox3.Text + "*"));
      
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count != 0)
            {

                // Zugehöriges Label ausdrucken


                label = new PrintDocument();
                PaperSize pS = new PaperSize("Custom Size", 620, 620);
                label.DefaultPageSettings.PaperSize = pS;
                label.PrinterSettings.PrinterName = comboBox3.Text;
                label.PrinterSettings.DefaultPageSettings.PaperSize = pS;
                label.PrintPage += new PrintPageEventHandler(label_PrintPage);

                label.Print();


            }


        }

        private void label_PrintPage(object sender, PrintPageEventArgs e)
        {

            SolidBrush brush = new SolidBrush(Color.Black);
            System.Drawing.Font header = new System.Drawing.Font(FontFamily.GenericSansSerif, 8.0F, FontStyle.Bold);
            Code128BarcodeDraw barcode128 = BarcodeDrawFactory.Code128WithChecksum;
            Image img;

            e.Graphics.DrawString("Venue: " + dataGridView1.SelectedRows[0].Cells["VenueDataGridViewTextBoxColumn"].Value.ToString(), header, brush, 5, 5);
            e.Graphics.DrawString("Manufacturer: " + dataGridView1.SelectedRows[0].Cells["ManufacturerDataGridViewTextBoxColumn"].Value.ToString(), header, brush, 5, 17);
            e.Graphics.DrawString("Model: " + dataGridView1.SelectedRows[0].Cells["ModelDataGridViewTextBoxColumn"].Value.ToString(), header, brush, 5, 29);
            e.Graphics.DrawString("SN: " + dataGridView1.SelectedRows[0].Cells["SeriennummerDataGridViewTextBoxColumn"].Value.ToString(), header, brush, 5, 41);
            e.Graphics.DrawString("Position: " + dataGridView1.SelectedRows[0].Cells["positionsIDDataGridViewTextBoxColumn"].Value.ToString(), header, brush, 5, 53);
            e.Graphics.DrawString("DMX: " + dataGridView1.SelectedRows[0].Cells["dmxAdressDataGridViewTextBoxColumn"].Value.ToString(), header, brush, 5, 65);
            e.Graphics.DrawString("Universe: " + dataGridView1.SelectedRows[0].Cells["universeDataGridViewTextBoxColumn"].Value.ToString(), header, brush, 5, 77);
            e.Graphics.DrawString("Fixture: " + dataGridView1.SelectedRows[0].Cells["fixture"].Value.ToString(), header, brush, 5, 89);

            //  Upcoming for fixturenumber e.Graphics.DrawString("Model: " + dataGridView1.SelectedRows[0].Cells["Model"].Value.ToString(), header, brush, 5, 5);

            // Logo
            e.Graphics.DrawImage(Worktool.Properties.Resources.ASC_Logo_quadratisch, 200, 10, 30, 30);
            // Barcode
            if (dataGridView1.SelectedRows[0].Cells["SeriennummerDataGridViewTextBoxColumn"].Value.ToString() != "")
            {
                img = barcode128.Draw(dataGridView1.SelectedRows[0].Cells["SeriennummerDataGridViewTextBoxColumn"].Value.ToString(), 30);
                e.Graphics.DrawImage(img, 5, 113);
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
